package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;
import java.util.*;


public class ConditionManager{
	
	private static Developer getApp(String appKey){
				
			return Developer.find("byAppKey",appKey).first();
			
			
	   }
	
	public static AppReward processAllConditions(String appKey){
		AppReward appReward = new AppReward();
		List<Level> levels = Level.find("byApplicationId",getApp(appKey)).fetch();
		//for(int i=0; i < l.count; i++)
		
		for(Level l : levels) {
		    if(l.execute()){
			   System.out.println("====== Level UP to "+ l.title);
			   
			   //idCurrent = l.id;
			  
			   AppPoint p = new AppPoint();
			   p.levelUp = true;
			   p.currentLevel = l.title;
			   p.type = l.point.title;
			   
			   System.out.println("processAllConditions addPoint type"+p.type);
			   appReward.addPoint(p.type,p);
				
			}
			l.save();
		}
		
		
		List<Condition> condition = Condition.find("byApplicationId",getApp(appKey)).fetch();
	
		for(Condition c : condition) {
		    if(c.execute()){
			   System.out.println("====== Badge Recived "+ c.badge.title);
			   appReward.badges.add(c.badge);
			}
			c.save();
		}
		
		return appReward;
	}
	
}

